﻿using UnityEngine;
using System;
using System.Collections;

public class DragItem : UIDragDropItem {

    //记录该物品数量的label
    public UILabel label;
    //记录物品数量
    public int num = 1;
    //保存该物品
    private Item thisItem;

    public Item ThisItem
    {
        get { return thisItem; }
        set { thisItem = value; }
    }

    protected override void OnDragDropRelease(GameObject surface)
    {        
        base.OnDragDropRelease(surface);
        //判断释放的位置是普通格子还是装备位还是其它位置
        //目标位置是格子
        if(surface.tag == "Cell")
        {
            this.transform.parent = surface.transform;
            this.transform.localPosition = Vector3.zero;
            ResetDepth(this.gameObject);
        }
        //目标位置是装备格子
        else if (surface.tag == "Equipment")
        {
            if (thisItem.ItemType == surface.transform.name)
            {
                this.transform.parent = surface.transform;
                this.transform.localPosition = Vector3.zero;
                ResetDepth(this.gameObject);
                //刚开始装备能叠加的判断方法
                //int num = int.Parse(this.transform.GetComponentInChildren<UILabel>().text);
                //if (num > 1)
                //{
                //    GameObject obj = NGUITools.AddChild(surface, this.transform.gameObject);
                //    obj.GetComponent<UISprite>().spriteName = this.transform.GetComponent<UISprite>().spriteName;
                //    obj.transform.localPosition = Vector3.zero;
                //    obj.GetComponentInChildren<UILabel>().text = 1 + "";

                //    this.transform.localPosition = Vector3.zero;
                //    int count = int.Parse(this.GetComponentInChildren<UILabel>().text);
                //    count--;
                //    this.GetComponentInChildren<UILabel>().text = count + "";

                //    ResetDepth(this.gameObject);
                //}
                //else
                //{
                //    this.transform.parent = surface.transform;
                //    this.transform.localPosition = Vector3.zero;
                //    ResetDepth(this.gameObject);
                //}

            }
            else
            {
                this.transform.localPosition = Vector3.zero;
                ResetDepth(this.gameObject);
            }
        }
        //如果目标位置是物品
        else if (surface.tag == "Item")
        {
            //目标物品的位置不是装备格子
            if (surface.transform.parent.tag != "Equipment")
            {
                //是否相同物品
                if (thisItem.Name == surface.GetComponent<DragItem>().thisItem.Name)
                {
                    
                    if (Item.IsOneOf(thisItem.ItemType) > 0)
                    {
                        //同为相同消耗品叠加在一起
                        int thisNum = int.Parse(this.GetComponentInChildren<UILabel>().text);
                        int surNum = surface.GetComponent<DragItem>().num;
                        surNum += thisNum;
                        surface.GetComponent<DragItem>().AddNum(surNum);
                        Destroy(this.gameObject);
                    }
                    else
                    {
                        //交换位置
                        Debug.Log("交换位置了哈，我跟你说，物品一样所以看不出来而已");
                        Transform parent = surface.transform.parent;
                        surface.transform.parent = this.transform.parent;
                        surface.transform.localPosition = Vector3.zero;
                        this.transform.parent = parent;
                        this.transform.localPosition = Vector3.zero;
                        ResetDepth(this.gameObject);
                    } 
                }
                else
                {
                    //交换位置
                    Transform parent = surface.transform.parent;
                    surface.transform.parent = this.transform.parent;
                    surface.transform.localPosition = Vector3.zero;
                    this.transform.parent = parent;
                    this.transform.localPosition = Vector3.zero;
                    ResetDepth(this.gameObject);
                }
            }
            //目标物品的位置是装备格子
            else
            {
                if (thisItem.ItemType == surface.transform.parent.name)
                {
                    //交换位置
                    Debug.Log("交换位置了哈，我跟你说，物品一样所以看不出来而已");
                    Transform parent = surface.transform.parent;
                    surface.transform.parent = this.transform.parent;
                    surface.transform.localPosition = Vector3.zero;
                    this.transform.parent = parent;
                    this.transform.localPosition = Vector3.zero;
                    ResetDepth(this.gameObject);
                }
                else
                {
                    //类型不符，拖拽的物品回到原来位置
                    this.transform.localPosition = Vector3.zero;
                    ResetDepth(this.gameObject);
                }
            }
            
        }
        else
        {
            this.transform.localPosition = Vector3.zero;
            ResetDepth(this.gameObject);
        }
        
    }
    //拖拽时提升拖拽物品的深度好让拖拽物品不被遮挡
    protected override void OnDragDropMove(Vector2 delta)
    {
        base.OnDragDropMove(delta);
        UISprite sp = this.gameObject.GetComponent<UISprite>();
        sp.depth = 4;
    }
    //释放物品是要修改回拖拽物品原来的深度，不然会导致混乱
    public void ResetDepth(GameObject obj)
    {
        UISprite sp = obj.GetComponent<UISprite>();
        sp.depth = 3;
    }
    //修改物品的数量
    public void AddNum(int n)
    {
        num += n;
        label.text = num + "";
    }

}
